package com.socket.server.serviceImpl;


import com.socket.server.config.OperationException;
import com.socket.server.config.ResultEnum;
import com.socket.server.controller.Operat;
import com.socket.server.mapper.OperationMapping;
import com.socket.server.service.OperationService;
import com.socket.server.utils.NettyServerHandler;
import io.netty.channel.ChannelId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @ProjectName: springboot_socket
 * @Package: com.sn.socket.serviceImpl
 * @ClassName: ${CLASS_NAME}
 * @Author: xiahuihui
 * @Description: ${description}
 * @Date: 2020/5/12 0012 10:39
 * @Version: 1.0
 */

@Service
public class OperationServiceImpl implements OperationService {

    @Resource
    private OperationMapping operationMapping;




    private static final Logger logger = LoggerFactory.getLogger(OperationServiceImpl.class);

//    private SockServer sockServer = SockServer.getInstance();


    @Override
    public void timelyTask(Operat operat) throws OperationException {

//        NettyServerHandler nettyServerHandler  = (NettyServerHandler) SpringUtil.getBean("com.socket.server.utils.NettyServerHandler");
        //                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //                    String createTime = df.format(new Date());
        //                    operationMapping.insertRealTime(deviceId,operat.getCommand(),operat.getFrequency(),operat.getTime(),createTime);
        //            try {
        //
        //                nettyTcpClient.sendMsg(message);
        //                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //                String createTime = df.format(new Date());
        //                operationMapping.insertRealTime(deviceId,operat.getCommand(),operat.getFrequency(),operat.getTime(),createTime);
        //            } catch (Exception e) {
        //                e.printStackTrace();
        //            }
        for (String deviceId : operat.getDeviceId()) {
            String message = operat.getCommand() + "#" + operat.getFrequency() + "#" + operat.getTime() + "#" + "A";
            logger.info(deviceId, message);
            //如果直接new一个Date对象，则采用的是系统时间
            Date systemDate = new Date();
            //指定格式为（年-月-日 时:分:秒）
            DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


            NettyServerHandler nettyServerHandler = new NettyServerHandler();
            ChannelId channelId = NettyServerHandler.deviceMap.get(deviceId);
            System.out.println(channelId.toString());
            if (channelId != null) {
                try {
                    nettyServerHandler.channelWrite(channelId, message);
                    SimpleDateFormat creatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    operationMapping.insertRealTime(deviceId,operat.getCommand(),operat.getFrequency(),operat.getTime(),systemDate);                } catch (Exception e) {
                    logger.error("发送即时任务异常！！！！");
                    throw new OperationException(ResultEnum.RESULT_SEND_MESSAGE_ERROR);
                }
            } else {
                logger.error("该设备未连接，找不到该设备！！！！");
                throw new OperationException(ResultEnum.RESULT_NOT_FOUND_DEVICE_ERROR);
            }


        }



    }
}
